package br.com.edoc.gerentes;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContextException;
import br.com.edoc.entidade.File;
import br.com.edoc.excecoes.ExceptionApplication;
import br.com.edoc.util.TratamentoData;

public class GerenteUploadIconesImpl implements GerenteUploadIcones{
	private static final String diretorioAtual="img/modulo";

	public String upload(File file) throws ExceptionApplication{
		// TODO Auto-generated method stub
		String idArquivo = TratamentoData.conveterDateForString(new Date());
		try {
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/")+""+diretorioAtual+"/"+idArquivo+"."+file.getExtencao()));
			
			byte[] bytes = new byte[file.getStream().available()];
			file.getStream().read(bytes);
			bufferedOutputStream.write(bytes);

			bufferedOutputStream.flush();
			bufferedOutputStream.close();
			return idArquivo+"."+file.getExtencao();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw new ApplicationContextException(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new ApplicationContextException(e.getMessage());
		}
		

	}
}